/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ants;
//import com.sun.xml.internal.ws.api.pipe.NextAction;
import java.io.*;
import java.awt.Point;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Scanner;
/**
 *
 * @author uhgy3k
 */
class Mapa {
    final int wallfix = 2;
    final int offset  = 1;
    int w;
    int h;
    int nplayers = 0;
    int food = 0;
    int[] maxAntsT;
    Point colony;
    Cells[][] cells;
    FileInputStream file;
    Scanner reader;
    int nclient;
    
    public void getInicial(Inicial in){
        int temp;
        maxAntsT[Constantes.explorer_id] = in.getExploradoras();
        maxAntsT[Constantes.warrior_id] = in.getGuerreras();
        maxAntsT[Constantes.worker_id] = in.getObreras();
        colony = in.getHormiguero();
        nclient = in.numCli;
        //Inviertes el hormiguero... poruqeee.... porque.... si...
        temp = colony.x;
        colony.x = colony.y;
        colony.y = temp;
        //colony.x += 1;
        //colony.y += 1;
        w = in.n;
        h = in.m;
        cells = new Cells[h][w];
        for(int i = 0; i < cells.length; i++){
            for(int j = 0; j < cells[0].length; j++){
                cells[i][j] = new Cells();
            }
        }
        //colony = new Point(w,h);
    }
    
    
    public Mapa(String path) throws IOException{
        cells = new Cells[w+wallfix][h+wallfix];
        maxAntsT = new int[3];
    }
    
    public void getAntsView(AntsView view, ArrayList<AntsBehave> ants){
        this.evaporate();
        ListIterator lst = ants.listIterator();
        while(lst.hasNext()){
            AntsBehave a = (AntsBehave)lst.next();
            Perspective p = (Perspective) view.perspectiva.get(a.id);
            // recuerda que y x
            //a.place.y+=1;
            //a.place.x+=1;
            //System.out.println(a.id + " x: "+a.place.x+ " y: "+a.place.y);
            for(int i = -1; i < 2; i++){
                for(int j = -1; j < 2; j++){   
                    cells[a.place.y+i][a.place.x+j].tipo = p.tiles[i+1][j+1].type;
                    cells[a.place.y+i][a.place.x+j].hasfood = p.tiles[i+1][j+1].food;
                    if(p.tiles[i+1][j+1].food == true){
                        cells[a.place.y+i][a.place.x+j].comida = 1;
                    }
                    else{
                        cells[a.place.y+i][a.place.x+j].comida = 0;
                    }
                }
            }
        }
        /*for(Cells ce[] : cells){
            for(Cells c : ce){
                if(c.tipo == Constantes.obstaculo)
                    System.out.print(" o ");
                else
                    System.out.print(" _ ");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();*/
    }
    
    private boolean isPlayer(String input){
        if(input.charAt(0) == 'u')
            return true;
        return false;
    }
    private boolean isFood(String input){
        if(input.charAt(0) == 'c'){
            food += Integer.parseInt(input.substring(1));
            return true;
        }
        return false;
    }
    public Celda getCelda(int x, int y){
        return cells[y][x];
    }
    public void setCelda(Cells cell, int x, int y){
        cells[y][x] = cell;
    }
    public Point getHormiguero(){
        return colony;
    }
    public Celda[][] getMap(){
        return cells;
    }
    
    public int maxGuerreras(){
        return maxAntsT[0];
    }
    public int maxExploradoras(){
        return maxAntsT[1];
    }
    public int maxObreras(){
        return maxAntsT[2];
    }
    public int maxJugadores(){
        return nplayers;
    }
    public int getTotalFood(){
        return food;
    }
    public void CommitCells(){
        for(int i = offset; i < (h+offset) && reader.hasNext(); i++){
            for(int j = offset; j < (w+offset); j++){
                if(cells[i][j].tipo != Constantes.obstaculo){
                    cells[i][j].commit();
                }
            }
        }
    }
    public void evaporate(){
        for (Cells c[] : cells){
            for(Cells ce : c){
               // System.out.println("asdf");
                ce.commit();
                for(int n = 0; n < Constantes.npheromone; n++){
                    ce.evapore(n);
                }
            }
        }
    }
    public void printmap(){
        for(int i = offset; i < (h+offset); i++){
            for(int j = offset; j < (w+offset); j++){
                if(cells[i][j].tipo == Constantes.obstaculo){
                    System.out.print('O');
                }
                else
                    System.out.print('_');
            }
            System.out.println();
        }
    }
}
